#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author: LiangChao
@email：kevinleong1011@hotmail.com
@desc: 
"""
import inspect
import os

from makit import fn

from .context import TestContext
from .fixture import TestFixture
from .manage import mk


def main(test=None, debug=None, **kwargs):
    """执行入口"""
    if not test:
        info = fn.get_caller_info()
        test = info.module
    if debug is None:
        env_items = dict((a, b) for a, b in os.environ.items() if a.find('IPYTHONENABLE') >= 0)
        debug = len(env_items) > 0  # 是否deug执行
    kwargs['debug'] = debug
    if not isinstance(test, TestFixture):
        project = mk.get_project(test)
        ctx = TestContext(**kwargs)
        f = TestFixture(project=project, context=ctx)
        if inspect.isfunction(test):
            cls = fn.get_class(test)
            if cls:
                node = f.add_test(cls)
                node.add_sub(test)
                f.total_count = 1
        else:
            f.add_test(test)
        f.run()
    else:
        test.context = TestContext(**kwargs)
        test.run()
